<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Home extends Base
{
    /**
     * 图片上传接口
     */
    public function upload($water = 0)
    {
        if (IsLogin()) {
            $file = request()->file('image');
            $url = DS.'static'.DS.'img'.DS.'uploads';
            $path = ROOT_PATH . 'public'.$url;
            $info = $file->move($path);

            if ($info) {
                $filename = $info->getSaveName();
                try {
                    $filePath = $path . DS . $filename;
                    $image = \think\Image::open($filePath);
                    if ($water == 1) {
                        $image
                            ->water(ROOT_PATH . '/public/static/water_logo.png', \think\Image::WATER_CENTER, 20)
                            ->thumb(600, 800)
                            ->save($filePath);
                    }
                    if (!empty(input('avatar'))) {
                        $image->thumb(120, 120)->save($filePath);
                    }
                } catch (\Exception $e) {
                }
                
                $url = $url.DS.$filename;

                return Result(1, '上传成功', [
                        'url' => $url
                    ]);
            } else {
                return Result(0, $file->getError());
            }
        } else {
            return Result(0, '无权限');
        }
    }
}